//
//  SEDoctorDetailHeaderView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/12.
//

import UIKit

let kDoctorDetailHeaderDefaultHeight: CGFloat = 190

class SEDoctorDetailHeaderView: UIView {
    
    var expandHandle: ((CGFloat, Bool) -> Void)?
    var hospitalPushClosure: (() -> Void)?
    
    private var diff: CGFloat = 0
    
    private var isExpanded = false
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .background
    }
    
    func updateWithViewModel(_ viewModel: SEDoctorDetailViewModel) {
        let multiLineHeight = viewModel.goodAt.height(width: UIScreen.screenWidth - 15 * 2, font: .regular(14))
        let singleLineHeight = viewModel.goodAt.heightForSingleLine(font: .regular(14))
        diff = multiLineHeight - singleLineHeight
        goodAtLabel.text = viewModel.goodAt
        nameLabel.text = viewModel.model.name
        hospitalLabel.text = viewModel.model.hospitalName
        jobAndDepartLabel.text = viewModel.jobAndDepart
        levelLabel.text = " \(viewModel.model.hospitalJb ?? " ") "
        levelLabel.isHidden = viewModel.isHiddenLevel
    }

    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var jobAndDepartLabel: UILabel!
    @IBOutlet private weak var hospitalLabel: UILabel!
    @IBOutlet private weak var levelLabel: UILabel! {
        didSet {
            levelLabel.layer.cornerRadius = 2
            levelLabel.layer.borderWidth = 1
            levelLabel.layer.borderColor = UIColor(hex: "#FFB32D").cgColor
        }
    }
    @IBOutlet private weak var goodAtLabel: UILabel!
    
    @IBOutlet private weak var avatarImageView: UIImageView!
    @IBOutlet private weak var hospitalButton: UIButton!
    @IBOutlet private weak var expandButton: UIButton! {
        didSet {
            expandButton.imageView?.transform = .identity
        }
    }
    
    @IBAction private func hospitalButtonAction() {
        hospitalPushClosure?()
    }
    
    @IBAction private func expandButtonAction() {
        isExpanded.toggle()
        expandHandle?(diff, isExpanded)
        self.goodAtLabel.numberOfLines = self.isExpanded ? (self.diff > 0 ? 0 : 1) : 1
    }
    
    func rotateExpandButton() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear) {
            self.expandButton.imageView?.transform = self.isExpanded ? CGAffineTransform(rotationAngle: .pi) : .identity
        }
    }
}
